package com.powernode.aspevct;

/**
 * @version 1.0
 * @Date 2024/8/17 9:53
 * @Author 付~~~
 */

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Date;

/**
 * 公共切面，AOP，记录系统操作各种功能的日志功能，基于AOP实现,用到环绕通知
 */

@Component
@Slf4j
@Aspect
public class SysLogAspect {
    /**
     * 切入点表达式
     */
    public static final String POINT_CUT = "execution (* com.powernode.controller.*.*(..))";

    @Around(value = POINT_CUT)
    public Object logAround(ProceedingJoinPoint joinPoint){
        Object result = null;
        // 获取请求对象
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();

        // 获取请求路径
        String path = request.getRequestURI();

        // 获取id地址
        String remoteHost = request.getRemoteHost();

        // 获取请求参数
        Object[] args = joinPoint.getArgs();

        // 获取请求方法,
            // (先拿到方法的签名)
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
            // 通过签名在拿到方法的对象
        Method method = signature.getMethod();
            // 拿到方法的名称
        String methodName = method.toString();

        // 获取目标方法上的API的Operation
        ApiOperation apiOperation = method.getDeclaredAnnotation(ApiOperation.class);

        // 判断该注解对象是否未空，
        String operation = "";
        if (ObjectUtil.isNotNull(apiOperation)){
            // 不为空，获取apiOperation注解描述
            operation = apiOperation.value();
        }


        String finalArgs = "";
        // 判断参数类型 [文件类型]
        if (ObjectUtil.isNotNull(args) && args.length !=0 && args[0] instanceof MultipartFile){
            // 说明当前参数为文件对象
            finalArgs = "file";
        }else {
            finalArgs = JSONObject.toJSONString(apiOperation);  //将参数对象转换为JSON格式字符串
        }


        // 记录开始时间
        long startTime = System.currentTimeMillis();

        try {
            // 执行方法
            result = joinPoint.proceed(args);
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }

        // 记录结束时间
        long endTime = System.currentTimeMillis();

        // 方法执行的时长
        long execTime = endTime - startTime;

        log.info("调用时间: {},请求接口路径:{},请求ip地址: {} ,方法名称 : {} ,执行时长 : {} ,方法的描述 : {}" ,
                new Date(),
                path,
                remoteHost,
                methodName,
                execTime,
                operation
                );

        return result;
    }
}
